﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using Vit.Common;
using Vit.Web.Providers;
using Vit.Web;
using Vit.Web.Mvc.DataAnnotations;
using Vit.Web.Models;

namespace Vit.Web.Models
{
    [Operation("List", "Design", 1, LinkUrlPattern = "/Page/Design/{Name}")]
    [Operation("List", "Manage", 2, LinkUrlPattern = "/Page/Manage/{Name}")]
    [Operation("List", "Preview", 3, LinkUrlPattern = "/Page/Preview/{Name}")]
    [Operation("List", "Remove", 4, LinkUrlPattern = "/Page/Delete/{Name}")]
    public class WebPage
    {
        [Key]
        [StringLength(50)]
        [Required]
        [UIHint("DialogLink", null, "url", "/Page/Edit/{Name}")]
        [ShowFor(ShowForFilter = true)]
        public string Name { get; set; }

        [StringLength(200)]
        [Required]
        public string UrlPattern { get; set; }

        [StringLength(200)]
        [Required]
        public string SampleUrl { get; set; }

        [Required]
        [ShowFor(false, true, true, false)]
        public int MatchPriority { get; set; }

        [StringLength(20)]
        [ShowFor(false, true, true, false)]
        public string CharSet { get; set; }

        [StringLength(20)]
        public string CultureInfo { get; set; }

        [StringLength(50)]
        [ShowFor(false, true, true, false)]
        public string PageTemplateName { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [ShowFor(ShowForFilter = true)]
        public string Title { get; set; }

        [StringLength(500)]
        [DataType(DataType.MultilineText)]
        [ShowFor(false, true, true, false)]
        public string MetaKeywords { get; set; }

        [StringLength(500)]
        [DataType(DataType.MultilineText)]
        [ShowFor(false, true, true, false)]
        public string MetaDescription { get; set; }

        [StringLength(2000)]
        [DataType(DataType.MultilineText)]
        [ShowFor(false, true, true, false)]
        public string Styles { get; set; }

        [StringLength(2000)]
        [DataType(DataType.MultilineText)]
        [ShowFor(false, true, true, false)]
        public string Scripts { get; set; }

        public virtual ICollection<PageWidget> Widgets { get; set; }

        public virtual WebSite WebSite { get; set; }

        public virtual WebPage BasePage { get; set; }

        private List<PageWidget> widgets;
        public List<PageWidget> GetWidgets()
        {
            if (widgets == null || widgets.Count <= 0)
            {
                widgets = new List<PageWidget>();
                WebPage parent = this;
                while (parent != null)
                {
                    if (parent.Widgets != null && parent.Widgets.Count > 0)
                        widgets.AddRange(parent.Widgets);
                    parent = parent.BasePage;
                }
                widgets = widgets.OrderBy(o => o.SectionName).ThenBy(o => o.Order).ToList();
            }

            return widgets;
        }
    }
}
